#!/usr/bin/python
import MuConnection, MuNetworkConfiguration, MuRestApi, time, sys, getopt  

USERNAME = 'admin'
PASSWORD = ''
MU = ''

#get the username and password
args = sys.argv[0:]
try:
    options, remainder = getopt.getopt(sys.argv[1:], "u:p:i:", ["username=", "password=","ipaddress="])
except getopt.GetoptError, err:
    # print help information and exit:
    print str(err) # will print something like "option -a not recognized"
    usage()
    sys.exit(2)
for o, p in options:
    if o in ("-u","--username"):
     	USERNAME = p
    elif o in ("-p", "--password"):
     	PASSWORD = p
    elif o in ("-i", "--ipaddress"):
        MU = p



myMuConnection = MuConnection.MuConnection("https://"+MU,USERNAME,PASSWORD)

scenarioUUID = "22872968-f3ae-4e2e-900e-808da25ada22"
scenarioFileName = 'myTemplate.xml'
analysisUuid = '1985d096-d06a-4063-902a-d3d952ba9a9e'
testRunUUID = '2e06fd95-15df-4b36-9630-e5f70ea007ac'
templateUUID = '22872968-f3ae-4e2e-900e-808da25ada22'


# establish new mu rest api
myRestApi = MuRestApi.MuRestApi(myMuConnection)
myRestApi.VERBOSE = True

if False:
	#------------ BACKUPS ----------------
	# run backup
	response = myRestApi.runBackup()
	jobId = response.getJobid()
	#jobId = '1277939227751'
	loop = True
	while loop:
		# check status
		response = myRestApi.getBackupStatus(jobId)
		status = response.getStatus()
		if status == 'Finished':
			loop = False
		time.sleep(2)
	# get backup file
	myRestApi.getBackup(jobId,"MyBackupFile.bin")


	#------------ TEMPLATES --------------
if False:	
	# list template types
	response = myRestApi.listTemplateTypes()
	templateTypeList = response.getRetrievedObject()
	
	# list templates
	for type in templateTypeList:
		print "LIST template type " + type
		myRestApi.listTemplates(type)
	
if False:	
	# import templates
	myRestApi.importTemplates(scenarioFileName)
	
	# export template
	# by uuid
	myRestApi.exportTemplates('myDownloadedTemplate.xml',templateUUID,)
	# by type and name
	myRestApi.exportTemplates('myDownloadedTemplate2.xml',None,"scenario","HTTP GET File request-Import")

	# delete template
	myRestApi.deleteTemplates(templateUUID)


if False:
	#------------ ARCHIVES ---------------
	# sart archive
	reponse = myRestApi.runArchive(analysisUuid)

	# check archive status
	jobId = reponse.getJobid()
	loop = True
	while loop:
		# check status
		response = myRestApi.getArchiveStatus(jobId)
		status = response.getStatus()
		if status == 'Finished':
			loop = False
		time.sleep(2)

	# get archive
	myRestApi.getArchive(jobId,"MyArchive.zip")
	time.sleep(2)
	
if False:
	# start archive with notes title and signed
	notes = "My Notes: Lets make sure we add commetns to our automated archive creation."
	title = "My Title: My AutoArchive Creation"
	evts = True
	pcaps = True
	logo = True
	audit = True
	samples = True
	sign = True
	reponse = myRestApi.runArchive(analysisUuid, title, notes, evts, pcaps, samples, logo, audit, sign)
	# check archive status
	jobId = reponse.getJobid()
	loop = True
	while loop:
		# check status
		response = myRestApi.getArchiveStatus(jobId)
		status = response.getStatus()
		if status == 'Finished':
			loop = False
		time.sleep(2)

	# get archive
	myRestApi.getArchive(jobId,"MyArchive2.zip")
	
	
if False:
	#------------ ANALYSIS ---------------
	# run analysis
	response = myRestApi.runAnalysis(testRunUUID)
	newUUID = response.getUUID()
	loop = True
	while loop:
		# check status
		response = myRestApi.getAnalysisStatus(newUUID)
		status = response.getStatus()
		if status == 'RUNNING':
			loop = False
		time.sleep(2)
	
	# pause analysis
	myRestApi.pauseAnalysis(newUUID)
	loop = True
	while loop:
		# check status
		response = myRestApi.getAnalysisStatus(newUUID)
		status = response.getStatus()
		if status == 'SUSPENDED':
			loop = False
		time.sleep(2)
	
	# resume analysis
	myRestApi.resumeAnalysis(newUUID)
	loop = True
	while loop:
		# check status
		response = myRestApi.getAnalysisStatus(newUUID)
		status = response.getStatus()
		if status == 'RUNNING':
			loop = False
		time.sleep(2)
	
	# abort analysis
	myRestApi.abortAnalysis(newUUID)
	loop = True
	while loop:
		# check status
		response = myRestApi.getAnalysisStatus(newUUID)
		status = response.getStatus()
		if status == 'ABORTED':
			loop = False
		time.sleep(2)
	
	# delete analysis
	myRestApi.deleteAnalysis(newUUID)
	time.sleep(5)
	
	# get analysis status
	myRestApi.getAnalysisStatus(analysisUuid)
	
if False:	
	# get analysis fault list
	response = myRestApi.getAnalysisFaultList(analysisUuid)
	faultList = response.getRetrievedObject()
	for fault in faultList:
		print fault


if False:
	#------------ CAPTURE ----------------
	# start a capture
	response = myRestApi.runCapture('a1')
	jobId = response.getJobid()
	time.sleep(10)
	
	# check capture status
	myRestApi.getCaptureStatus(jobId)
	time.sleep(10)
	
	# get/stop a capture
	myRestApi.getCapture(jobId,'myPacketCap.pcap')
